package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.ChartMapper;
import com.example.demo.model.Chart;
import com.example.demo.service.ChartService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
        implements ChartService {

    public Chart GetLastChart(int userId) {
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        queryWrapper.orderByDesc("updateTime");
        // 限制结果只取第一条，即最新的一条记录
        queryWrapper.last("LIMIT 1");
        Chart latestChart = this.baseMapper.selectOne(queryWrapper);

        return latestChart;
    }

    public List<Chart> GetChartPagination(int userId, Integer pageNum) {
        // 每页显示的记录数
        // 创建Page对象，设置当前页码和每页显示数量
        Page<Chart> page = new Page<>(pageNum, 4);

        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        // 执行分页查询
        IPage<Chart> chartIPage = this.baseMapper.selectPage(page, queryWrapper);
        return  chartIPage.getRecords();
    }


    public Long GetUserChartNum(int userId) {
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userId);
        return this.baseMapper.selectCount(queryWrapper);
    }

    //只是将IsDelete置为True
    public Integer DeleteChartById(int chartId) {
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", chartId);
        int result = this.baseMapper.delete(queryWrapper);
        return result; // 返回删除操作影响的行数，一般情况下，如果删除成功则返回1
    }

    //根据图表名字查找

    public List<Chart> queryByChartName(int userId, String chartName) {
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        queryWrapper.eq("name",chartName);
        return this.baseMapper.selectList(queryWrapper);
    }

    public Chart queryChartByChartId(int chartId) {
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",chartId);
        return this.baseMapper.selectOne(queryWrapper);
    }

    public Integer updateChartData(int chartId, String chartData) {
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",chartId);
        Chart chart=this.baseMapper.selectOne(queryWrapper);
        chart.setChartData(chartData);
        Date now=new Date();
        chart.setUpdateTime(now);
        int result=this.baseMapper.updateById(chart);
        return result;
    }

    public Integer updateChartInfo(int chartId, String chartName, String genResult) {
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",chartId);
        Chart chart=this.baseMapper.selectOne(queryWrapper);
        chart.setGenResult(genResult);
        chart.setName(chartName);
        Date now=new Date();
        chart.setUpdateTime(now);
        int result=this.baseMapper.updateById(chart);
        return result;
    }

    public List<Chart> queryChartByUserId(int userId) {
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        queryWrapper.last("LIMIT 4");
        return this.baseMapper.selectList(queryWrapper);
    }

    public List<Chart> queryChartByStatus(int userId, String status) {
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        queryWrapper.ne("status",status);
        return this.baseMapper.selectList(queryWrapper);
    }
}
